package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.RemindAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.Remind;
import com.xywy.bloodandroid.util.FileUtils;

/*******************************************
 * @Title: RemindListActivity.java
 * @Package com.xywy.bloodandroid.act
 * @Description: TODO用药提醒列表
 * @author Benwolf
 * @date 2014-12-29 下午2:08:37
 * @version V1.0
 ********************************************/
public class RemindListActivity extends BaseActivity {

	private RelativeLayout back;
	private TextView title;
	private ImageView right_img;
	private LinearLayout ll_remind_item;
	private ListView lv_medications_remind;
	// private TextView tv_unit;
	// private TextView tv_times;
	// private TextView tv_medicine_name;
	private List<Remind> reminds = new ArrayList<Remind>();
	private Handler deleteHandler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0x1101) {
				View v = (View) msg.obj;
				int position = msg.arg1;
				showDeleteAlert(v, position);
			}
		};
	};
	private RemindAdapter adapter;

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_remind_list);
		back = (RelativeLayout) findViewById(R.id.left_img);
		title = (TextView) findViewById(R.id.middle_title);
		right_img = (ImageView) findViewById(R.id.right_img);
		right_img.setVisibility(View.VISIBLE);
		ll_remind_item = (LinearLayout) findViewById(R.id.ll_remind_item);
		lv_medications_remind = (ListView) findViewById(R.id.lv_medications_remind);
		reminds = FileUtils.getList("reminds");
		System.out.println("++++++++++"+reminds.size());
		adapter = new RemindAdapter(context, reminds, deleteHandler);
		lv_medications_remind.setAdapter(adapter);
		// tv_times = (TextView) findViewById(R.id.tv_times);
		// tv_medicine_name = (TextView) findViewById(R.id.tv_medicine_name);
		// tv_unit = (TextView) findViewById(R.id.tv_unit);

	}

	private void showDeleteAlert(View v, final int position) {
		final Dialog dialog = new Dialog(this, R.style.Translucent_NoTitle);
		dialog.setContentView(R.layout.wjk_delete_menu);
		TextView delete = (TextView) dialog.findViewById(R.id.delete);
		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// weightDataDao.delete(data.get(position));
				reminds.remove(reminds.get(position));
				FileUtils.saveObjectList("reminds", reminds);
				adapter.notifyDataSetChanged();
				dialog.dismiss();
			}
		});
		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
		// 显示的坐标
		lp.x = v.getWidth() / 2;

		lp.y = v.getTop();
		dialogWindow.setAttributes(lp);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		back.setOnClickListener(this);
		right_img.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.right_img:// 右上角添加
			Intent intent = new Intent(this, EditRemindActivty.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			String time = data.getStringExtra("time");
			String medicine_name = data.getStringExtra("medicine_name");
			String dosage = data.getStringExtra("dosage");
			switch (requestCode) {
			case 1:
				// if (time.length() > 0)
				// tv_times.setText(time);
				// if (medicine_name.length() > 0)
				// tv_medicine_name.setText(medicine_name);
				// if (dosage.length() > 0)
				// tv_unit.setText(dosage);

				break;

			default:
				break;
			}
		}

	}

	@SuppressLint("NewApi") @Override
	protected void initData() {
		// TODO Auto-generated method stub
		title.setText(getResources().getString(
				R.string.take_medicine_remind_str));
		right_img.setBackground(getResources()
				.getDrawable(R.drawable.plus_icon));
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		reminds = FileUtils.getList("reminds");
		System.out.println("++++++++++"+reminds.size());
		adapter = new RemindAdapter(context, reminds, deleteHandler);
		lv_medications_remind.setAdapter(adapter);
	}

}
